package com.java8feature.java8action.chapter11.entity;

import lombok.Data;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/6/16 15:37
 */
@Data
public class Shop {
    public Shop(String fruitName) {
        this.name = fruitName;
    }

    public double getPrice(String product) throws InterruptedException {
        return calculatePrice(product);
    }

    private double calculatePrice(String product) throws InterruptedException {
        Random r = new Random();
        //int randNum =  r.nextInt(3);
        long startTime = System.currentTimeMillis();

        delay(1);
        System.out.println("get" + product + "Price Time ->" + (System.currentTimeMillis() - startTime));
        return r.nextDouble() * product.charAt(0) + product.charAt(1);
    }

    private void delay(Integer dalaySeconds) throws InterruptedException {
        if (dalaySeconds == null) {
            delay1SECNODS();
        } else {
            TimeUnit.SECONDS.sleep(dalaySeconds);
        }
    }

    private void delay1SECNODS() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
    }

    public String name;
}
